<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>查看设备详情</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		$(document).ready(function() {
			//$("#name").focus();
			$("#inputForm").validate({
				submitHandler: function(form){
					loading('<spring:message code="common.subbmiting"/>');
                    timeVali($("#btnSubmit"));
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
		});
        function createFloorMapping(){
            var insertFloor = $("#insertFloor");
            var insertFloorVal = parseInt(insertFloor.val());

            var isInsert = false;

            var divTrs = $("#floor_mapping_div");
            	divTrs.html("");
                var insert_tr = $("#insert_tr").children().children();
				for(var i=0;i<insertFloorVal;i++){
					console.log(i);
                var insterTr = insert_tr.clone();

                insterTr.find(".floor").val(i+1);
                if(i==0){
                    insterTr.find(".to").val("-1");
                }else{
                    insterTr.find(".to").val(i);
                }

                $("#floor_mapping_div").append(insterTr);
				
			}




            insertFloor.val("");
        }
        function deleteCol(obj){
            var thisTr = $(obj).parent().parent();
            thisTr.nextAll().each(function(){
                var floorInput = $(this).find(".floor");

                floorInput.val(parseInt(floorInput.val())-1);
            });

            thisTr.remove();
        }
        function insertCol(obj){
            var thisTr = $(obj).parent().parent();

            var addTr = thisTr.clone();
            addTr.find(".to").val("");
            thisTr.before(addTr);

            addTr.nextAll().each(function(){
                var floorInput = $(this).find(".floor");

                floorInput.val(parseInt(floorInput.val())+1);
            });
        }
	</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li class="active"><a href="javascript:void(0);">楼层映射</a></li>
	</ul>
	<form:form id="inputForm" modelAttribute="eleEquipment" action="${ctx}/equipment/eleEquipment/floorsave" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<sys:message content="${message}"/>

		<div class="control-group">
            <label class="control-label">请输入楼层数：</label>
            <div class="controls">
                <label class="lbl"><input id="insertFloor" type="text " class="number"></label>
                <a onclick="createFloorMapping()" class="btn btn-primary">生成映射</a>
            </div>
		</div>

		<div class="control-group">
            <table id="contentTable" class="table table-striped table-bordered table-condensed" style="width: 400px;">
                <thead>
                <tr><th>物理楼层</th> <th>显示楼层</th> <th><spring:message code='common.operation'/></th></tr>
                </thead>
                <tbody id="floor_mapping_div">
                    <c:forEach items="${floorMapping}" var="floor" varStatus="status">
                        <tr>
                            <td><input name="floor.floor" value="${floor.floor}" class="floor input-mini" style="width: 50px;" readonly></td>
                            <td><input name="floor.to" value="${floor.to}" class="to input-mini" style="width: 50px;"></td>
                            <td>
                                <a onclick="deleteCol(this)" class="btn-link"><spring:message code='common.delete'/></a>
                                <a onclick="insertCol(this)" class="btn-link">插入</a>
                            </td>
                        </tr>
                    </c:forEach>
                </tbody>
            </table>
		</div>

        <div class="control-group">
            <input id="btnSubmit" class="btn btn-primary" type="submit" value="<spring:message code='common.save'/>"/>
        </div>
	</form:form>

    <table id="insert_tr" style="display: none;">
        <tr>
            <td><input name="floor.floor" class="floor input-mini" style="width: 50px;" readonly></td>
            <td><input name="floor.to" class="to input-mini" style="width: 50px;"></td>
            <td>
                <a onclick="deleteCol(this)" class="btn-link"><spring:message code='common.delete'/></a>
                <a onclick="insertCol(this)" class="btn-link">插入</a>
            </td>
        </tr>
    </table>
</body>
</html>